package com.ethan.common.orm.service;

import java.io.Serializable;
import java.util.Map;

import org.springframework.transaction.annotation.Transactional;

import com.ethan.common.exception.ServiceException;
import com.ethan.common.orm.Page;
import com.ethan.common.orm.PropertyFilter.MatchType;
import com.ethan.common.orm.dao.hibernate.HibernateDao;

@Transactional
public abstract class BaseService<T,PK extends Serializable> implements IBaseService<T,PK>{

	public void update(T entity) throws ServiceException {
		getEntityDao().update(entity);
	}

	public void saveOrUpdate(T entity) throws ServiceException {
		getEntityDao().saveOrUpdate(entity);
	}

	public T getById(PK id) throws ServiceException {
		return getEntityDao().get(id);
	}

	public T loadById(PK id) throws ServiceException {
		return getEntityDao().load(id);
	}

	public void delete(T entity) throws ServiceException {
		getEntityDao().delete(entity);
	}

	public void deleteById(PK id) throws ServiceException {
		getEntityDao().delete(id);
	}

	public void deleteAllByIds(PK[] ids) throws ServiceException {
		getEntityDao().deleteAllByIds(ids);
	}

	public void save(T entity) throws ServiceException {
		getEntityDao().save(entity);
	}

	public Page<T> findAll(Page<T> page) {
		return getEntityDao().getAll(page);
	}

	public Page<T> findByProperty(String[] names, String[] values)
			throws ServiceException {
		
		return null;
	}

	public Page<T> findByProperty(Page<T> page,String name, String value) throws ServiceException {
		return getEntityDao().findBy(page,name, value,MatchType.EQ);
	}

	@Override
	public T findUnique(String hql, Object... values) throws ServiceException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public T findUnique(String hql, Map<String, ?> values)
			throws ServiceException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean isUnique(String propertyName, Object newValue,
			Object oldValue) throws ServiceException {
		// TODO Auto-generated method stub
		return false;
	}
	
	public abstract HibernateDao<T,PK> getEntityDao();
}
